<template>
  <div class="system-ys7Cameras-edit">
    <el-dialog v-model="isShowDialog" width="500px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-ys7Cameras-edit .el-dialog', '.system-ys7Cameras-edit .el-dialog__header']">添加预置点</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="预置点名称" prop="indexName">
          <el-input v-model="formData.indexName" placeholder="请输入预置点名称"/>
        </el-form-item>
        <el-form-item label="停留时间" prop="timeout">
          <el-input v-model="formData.timeout" type="number" placeholder="请输入停留时间"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {reactive, toRefs, defineComponent, ref, unref, getCurrentInstance, onMounted} from 'vue';
import {ElMessage,} from 'element-plus';
import {
  addBusPresettingBit, BusPresettingBitEdit
} from "/@/api/system/ys7Cameras";
export default defineComponent({
  name: "apiV1SystemYs7CamerasEdit",
  components: {},
  props: {
    statusOptions: {
      type: Array,
      default: () => []
    },
  },
  setup(props, {emit}) {
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();
    const state = reactive({
      loading: false,
      isShowDialog: false,
      formData: {
        indexName: '',
        timeout: 10,
        id: ''
      },
      // 表单校验
      rules: {
        indexName: [
          {required: true, message: "预置点名称不能为空", trigger: "blur"}
        ],
        timeout: [
          {required: true, message: "停留时间不能为空", trigger: "blur"}
        ],
      }
    });
    // 打开弹窗
    const openDialog = (row:any) => {
      resetForm();
      state.formData = row
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      resetForm();
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          if (state.formData.id) {
            BusPresettingBitEdit(state.formData)
                .then((res: any) => {
                  if (res.code == 0) {
                    ElMessage.success('更新成功');
                    emit('busPresettingBitList')
                    onCancel()
                  } else {
                    ElMessage.error(res.message);
                  }
                })
          } else {
            addBusPresettingBit(state.formData)
                .then((res: any) => {
                  if (res.code == 0) {
                    ElMessage.success('添加成功');
                    emit('busPresettingBitList')
                    onCancel()
                  } else {
                    ElMessage.error(res.message);
                  }
                })
          }
        }
      });
    };
    const resetForm = () => {
      state.formData = {
        indexName: '',
        timeout: 10,
        id: ''
      }
    };
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>
</style>
